/**
 * 
 */
package vn.edu.fpt.xml.hrm.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Yoshimi
 * 
 */
public abstract class AbstractController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1803543216389364242L;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private boolean redirect;
	private boolean forward;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doDelete(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected final void doDelete(HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doDelete(req, resp);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doHead(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected final void doHead(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doHead(req, resp);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doOptions(javax.servlet.http.
	 * HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected final void doOptions(HttpServletRequest arg0,
			HttpServletResponse arg1) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doOptions(arg0, arg1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doPut(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected final void doPut(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPut(req, resp);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doTrace(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected final void doTrace(HttpServletRequest arg0,
			HttpServletResponse arg1) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doTrace(arg0, arg1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServlet#getLastModified(javax.servlet.http.
	 * HttpServletRequest)
	 */
	@Override
	protected final long getLastModified(HttpServletRequest req) {
		// TODO Auto-generated method stub
		return super.getLastModified(req);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected final void service(HttpServletRequest arg0,
			HttpServletResponse arg1) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(arg0, arg1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest,
	 * javax.servlet.ServletResponse)
	 */
	@Override
	public final void service(ServletRequest arg0, ServletResponse arg1)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(arg0, arg1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected final void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected final void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println(this.getClass().getName() + " - start");
		String targetPage = null;
		request = req;
		response = resp;
		redirect = false;
		forward = true;
		try {
			targetPage = execute();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new ServletException(e);
		}
		if (redirect) {
			resp.sendRedirect(targetPage);
		} else if (forward) {
			req.getRequestDispatcher(targetPage).forward(req, resp);
		}
		System.out.println(this.getClass().getName() + " - end");
	}

	/**
	 * @return the request
	 */
	protected HttpServletRequest getRequest() {
		return request;
	}

	/**
	 * @return the response
	 */
	protected HttpServletResponse getResponse() {
		return response;
	}

	/**
	 * Set whether to use redirect to the next page.<br>
	 * If set to true, redirect will be used.<br>
	 * By default, forward will be used.<br>
	 * NOTE: request will be cleared on redirect<br>
	 * 
	 * @param redirect
	 *            if set to true, redirect will be used to go to the next page
	 */
	protected void setRedirect(boolean redirect) {
		this.redirect = redirect;
	}

	/**
	 * Set whether to use forward to the next page.<br>
	 * If set to true, forward will be used.<br>
	 * By default, forward will be used.<br>
	 * NOTE: if <tt>redirect</tt> is set to true, this will be ignored 
	 * 
	 * @param forward if set to true, forward will be used to go to the next page
	 */
	protected void setForward(boolean forward) {
		this.forward = forward;
	}

	/**
	 * 
	 * @return the url to next page
	 */
	abstract protected String execute() throws Exception;

}
